package article;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import objects.ArticleItem;

import connect.MyConnect;

public class ArticleControl {
	private MyConnect mc;
	private Connection con;
	public ArticleControl(){
		mc = new MyConnect();
		this.con = getConnect();
	}
	public Connection getConnect(){
		return mc.getConnect();
	}
	
	
	//phuong thuc lay toan bo bai viet
	public ArrayList getArticleItems(){
		ArrayList list = new ArrayList();
		ArticleItem item = null;
		try {
			PreparedStatement pre = this.con.prepareStatement("SELECT * FROM tblarticle");
			ResultSet rs = pre.executeQuery();
			if(rs!=null){
				while(rs.next()){
					item = new ArticleItem();
					item.setArticle_id(rs.getInt("article_id"));
					item.setArticle_title(rs.getString("article_title"));
					item.setArticle_category_id(rs.getShort("article_category_id"));
					item.setArticle_content(rs.getString("article_content"));
					item.setArticle_section_id(rs.getShort("article_section_id"));
					list.add(item);
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return list;
	}
	

	//lay bai viet theo ma id
	public ArticleItem getArticleItem(int id){
		ArticleItem item = new ArticleItem();
		String sql=" SELECT * FROM tblarticle WHERE article_id = "+id;
		try {
			PreparedStatement pre = this.con.prepareStatement(sql);
			ResultSet rs = pre.executeQuery();
			if(rs!=null){
				while(rs.next()){
					item = new ArticleItem();
					item.setArticle_id(rs.getInt("article_id"));
					item.setArticle_title(rs.getString("article_title"));
					item.setArticle_category_id(rs.getShort("article_category_id"));
					item.setArticle_content(rs.getString("article_content"));
					item.setArticle_section_id(rs.getShort("article_section_id"));
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return item;
	}
	
	public static void main(String args[]){
		ArticleControl ac = new ArticleControl();
		
	}
	
}
